package com.qf.dao.impl;

import com.qf.dao.AddressDao;
import com.qf.domain.Address;
import com.qf.util.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Administrator
 * @version v1.0
 * @date 2022/4/20
 */
public class AddressDaoImpl implements AddressDao {

    @Override
    public List<Address> findAddListByUid(int uid) {
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDatasource());
        String sql = "select id , detail, name , phone, uid, level from tb_address where uid=?";
        try {
            List<Address> addList = qf.query(sql, new BeanListHandler<>(Address.class), uid);
            return addList;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询地址列表失败!!");
        }
        return null;
    }

    @Override
    public void insert(Address address) {
        String sql = "INSERT INTO `tb_address` (`id`, `detail`, `name`, `phone`, `uid`, `level`) VALUES (null, ?, ?, ?, ?, ?)";
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDatasource());
        try {
            qf.update(sql , address.getDetail(), address.getName(), address.getPhone(), address.getUid(), address.getLevel());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("新增失败!!");
        }
    }

    @Override
    public void deleteById(String id) {
        String sql = "delete from tb_address where id =?";
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDatasource());
        try {
            qf.update(sql , id);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("删除失败!!");
        }
    }

    @Override
    public void update(Address address) {
        String sql = "UPDATE `tb_address` SET `detail`=?, `name`=?, `phone`=?, `uid`=?, `level`=? WHERE (`id`=?)";
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDatasource());
        try {
            qf.update(sql , address.getDetail(), address.getName(), address.getPhone(),address.getUid(),address.getLevel(), address.getId());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("修改失败!!");
        }
    }

    @Override
    public void clearDefaultAddress(int uid) {
        Connection conn = DataSourceUtils.getConnection();

        String sql = "UPDATE `tb_address` SET `level`= 0 WHERE (`uid`=?)";
        QueryRunner qf = new QueryRunner();
        try {
            qf.update(conn, sql , uid);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("修改失败!!");
        }
    }

    @Override
    public void setDefaultAddress(String aid) {
        Connection conn = DataSourceUtils.getConnection();

        String sql = "UPDATE `tb_address` SET `level`= 1 WHERE (`id`=?)";
        QueryRunner qf = new QueryRunner();
        try {
            qf.update(conn, sql , aid);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("修改失败!!");
        }
    }

    @Override
    public long findCountByUid(int id) {
        QueryRunner qf = new QueryRunner(DataSourceUtils.getDatasource());
        String sql = "select count(0) from tb_address where uid=?";
        try {
           long count=(long)   qf.query(sql,new ScalarHandler(), id);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询失败!!");
        }
        return 0;
    }
}
